#include "UpgradeStarConsumeCacheData.h"


UpgradeStarConsumeCacheData* UpgradeStarConsumeCacheData::mInstance = NULL;

void UpgradeStarConsumeCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new UpgradeStarConsumeCacheData();
	}
}

UpgradeStarConsumeCacheData* UpgradeStarConsumeCacheData::GetInstance()
{
	return mInstance;
}

UpgradeStarConsumeCacheData::UpgradeStarConsumeCacheData()
	: m_maxCount(0)
{
}

UpgradeStarConsumeCacheData::~UpgradeStarConsumeCacheData()
{
	Clear();
}

bool UpgradeStarConsumeCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void UpgradeStarConsumeCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool UpgradeStarConsumeCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/hero/upgradeStarConsume.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/hero/upgradeStarConsume.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}

void UpgradeStarConsumeCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		UpgradeStarConsume* p = new UpgradeStarConsume;
		_safe_check(p);

		p->mId = jm["star"].asUInt();
		p->mCallStoneNum = jm["call_stone_num"].asUInt();
		p->mEvolveCost = jm["evolve_cost"].asUInt();
		p->mEvolveStoneNum = jm["evolve_stone_num"].asUInt();
		p->mConvertStoneNum = jm["convert_stone_num"].asUInt();

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

UpgradeStarConsume* UpgradeStarConsumeCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool UpgradeStarConsumeCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	UpgradeStarConsume* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error18");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["call_stone_num"] = Json::Value(p->mCallStoneNum);
	_jm["evolve_cost"] = Json::Value(p->mEvolveCost);
	_jm["evolve_stone_num"] = Json::Value(p->mEvolveStoneNum);
	_jm["convert_stone_num"] = Json::Value(p->mConvertStoneNum);

	_safe_guard_end;

	return true;
}

uint32_t UpgradeStarConsumeCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}

